package com.itheima.chat.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itheima.chat.R;


/**
 * Created by Administrator on 2015/3/30.
 */
public class TabIndicatorView extends RelativeLayout
{
    private TextView  mTvTitle;
    private TextView  mTvUnread;
    private ImageView mIvIcon;
    private boolean   mSelected;//是否选中
    private int       mNormalResId;//默认时的图片资源
    private int       mSelectedResId;//选中时的图片资源

    public TabIndicatorView(Context context)
    {
        super(context);

        initView();
    }
    public TabIndicatorView(Context context,
                            AttributeSet attrs)
    {
        super(context,
              attrs);

        initView();
    }

    private void initView()
    {
        View.inflate(getContext(),
                     R.layout.tab_indicator,
                     //将xml挂载到当前的类中
                     this);

        mTvTitle = (TextView) findViewById(R.id.tab_indicator_tv_title);
        mTvUnread = (TextView) findViewById(R.id.tab_indicator_tv_unread);
        mIvIcon = (ImageView) findViewById(R.id.tab_indicator_iv_icon);

        //设置没有未读数
        setTabUnread(0);
    }

    public void setTabTitle(String title)
    {
        mTvTitle.setText(title);
    }

    public void setTabUnread(int unread)
    {
        if (unread <= 0)
        {
            mTvUnread.setVisibility(View.GONE);
        } else
        {
            mTvUnread.setVisibility(View.VISIBLE);

            if (unread >= 100)
            {
                mTvUnread.setText("99+");
            } else
            {
                mTvUnread.setText(unread + "");
            }
        }
    }

    public void setTabIcon(int normalResId,
                           int selectedResId)
    {
        this.mNormalResId = normalResId;
        this.mSelectedResId = selectedResId;

        mIvIcon.setImageResource(normalResId);
    }

    public void setTabSelected(boolean selected)
    {
        this.mSelected = selected;
//
//        if (selected)
//        {
//            //选中时
//            mIvIcon.setImageResource(mSelectedResId);
//        } else
//        {
//            //选中时
//            mIvIcon.setImageResource(mNormalResId);
//        }
        mIvIcon.setImageResource(mSelected ? mSelectedResId : mNormalResId);
    }

}
